import { GSprite } from "../../../../com/mui/controls/GSprite";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { EventName } from "../../mvc/EventName";
import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { Cache } from "../../cache/Cache";
import { RechargeShowListRenderer } from "./RechargeShowListRenderer";
import { ScrollPolicy } from "../../../../flashx/textLayout/container/ScrollPolicy";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { RechargeData } from "../loginNotice/OpenRecharge/RechargeData";
import { ObjectItem } from "../treasure/ObjectItem";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { RechargeShowLabelItem } from "./RechargeShowLabelItem";
import { SecTimer } from "../../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../../com/gengine/core/frame/TimerType";
import { StatueUserPlayer } from "../../scene/player/entity/StatueUserPlayer";
import { DirectionType } from "../../scene/modle/data/DirectionType";
import { MountModelPanel } from "../mounts/MountModelPanel";
import { SWFPlayer } from "../../scene/modle/SWFPlayer";
import { GiftBagInfo } from "../../model/GiftBagInfo";
import { ECategory } from "../../../../Message/Public/ECategory";
import { EProp } from "../../../../Message/Public/EProp";
import { PetConfig } from "../../resource/PetConfig";
import { ModelUtil } from "../../scene/player/info/ModelUtil";
import { ModelType } from "../../scene/modle/data/ModelType";
import { EEquip } from "../../../../Message/Public/EEquip";
import { EEntityAttribute } from "../../../../Message/Public/EEntityAttribute";
import { ColorConfig } from "../../resource/ColorConfig";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { TOpenRechargeGiftBag } from "../../../../Message/Db/Tables/TOpenRechargeGiftBag";
import { ItemData } from "../../resource/info/ItemData";
import { Language } from "../../../../extend/language/Language";
import { DataEvent } from "../../events/DataEvent";
import { GTileList } from "../../../../com/mui/controls/GTileList";
import { GTextFiled } from "../../../../com/mui/controls/GTextFiled";
type int = number;
//class RechargeShowListPanel
    
    export  class RechargeShowListPanel extends GSprite
    {
        constructor()
        {
            super();
            this._labelIdxArr = [[1, 2, 3], [4, 3, 6], [1, 7, 8], [9, 10, 11]];
            this._labelItems = [];
            this.init();
            Dispatcher.addEventListener(EventName.RechargeGiftBagPanelItemSelect, this.onRechargeGiftBagPanelItemSelectHandler);
            return;
        }

        protected init(): void
        {
            var loc1=ObjCreate.createBg(0, 0, 245, 470, this, ImagesConst.WindowCenterA);
            var loc2=ObjCreate.createBg(5, 5, 235, 460, this, ImagesConst.WindowCenterB);
            addChildAt(loc1, 0);
            addChildAt(loc2, 1);
            ObjCreate.createGImageBitmap(ImagesConst.RobCityRoleBg, 50, 85, this);
            ObjCreate.createBg(10, 382, 225, 2, this, ImagesConst.SplitLine);
            this._sex = Cache.instance.role.entityInfo.sex;
            this._camp = Cache.instance.role.entityInfo.camp;
            this._career = Cache.instance.role.entityInfo.career;
            this._tileList = ObjCreate.createTileList(8, 390, 235, 70, this);
            this._tileList.setStyle("cellRenderer", RechargeShowListRenderer);
            this._tileList.columnWidth = 58;
            this._tileList.scrollPolicy = ScrollPolicy.OFF;
            this._tileList.dataProvider = new DataProvider(RechargeData.getRechargeValuables());
            this._tileList.selectedIndex = 0;
            this._tileList.addEventListener(flash.events.Event.CHANGE, this.onChangeHandler);
            this.createModel();
            this.createLabelItem();
            this._codeMap = RechargeData.codeMap;
            this._infoMap = RechargeData.infoMap;
            this._itemNum = this._tileList.numChildren;
            this._objItem = new ObjectItem();
            this._objItem.move(75, 38);
            this._objItem.setSize(32, 32);
            addChild(this._objItem);
            this._nameTxt = ObjCreate.createGTextField("", 115, 45, 150, 20, this);
            this._tipsTxt = ObjCreate.createGTextField("", 5, 12, 220, 20, this, GlobalStyle.setTextFormatAlign(GlobalStyle.textFormat1, "right"));
            this.setShowModel(0);
            this.createTimer();
            return;
        }

        /* internal  */createLabelItem(): void
        {
            var loc1=null;
            var loc2=30;
            var loc3=317;
            var loc4=0;
            while (loc4 < 3) 
            {
                loc1 = new RechargeShowLabelItem();
                loc1.x = loc2;
                loc1.y = loc3;
                loc2 = loc2 + 65;
                addChild(loc1);
                this._labelItems.push(loc1);
                ++loc4;
            }
            return;
        }

        /* internal  */updateLabelItem(arg1: int): void
        {
            var loc3=null;
            var loc1=this._labelIdxArr[arg1];
            var loc2=this._labelItems.length;
            var loc4=0;
            while (loc4 < loc2) 
            {
                (loc3 = this._labelItems[loc4]).updateLabel(loc1[loc4]);
                ++loc4;
            }
            return;
        }

        protected createTimer(): void
        {
            this._secTimer = new SecTimer(2);
            this._secTimer.addListener(TimerType.ENTERFRAME, this.onTimerEnterFrameHandler);
            this._secTimer.addListener(TimerType.COMPLETE, this.onTimerCompleteHandler);
            this.startTimer();
            return;
        }

        protected createModel(): void
        {
            if (this._userModel == null) 
            {
                this._userModel = new StatueUserPlayer();
                this._userModel.direction = DirectionType.South;
                this._userModel.sex = this._sex;
                this._userModel.camp = this._camp;
                this._userModel.career = this._career;
                this._userModel.x = 120;
                this._userModel.y = 270;
                this._userModel.visible = false;
                addChildAt(this._userModel, 3);
            }
            if (this._mountModel == null) 
            {
                this._mountModel = new MountModelPanel();
                this._mountModel.mouseChildren = false;
                this._mountModel.mouseEnabled = false;
                this._mountModel.x = 20;
                this._mountModel.y = 270;
                this._mountModel.visible = false;
                addChildAt(this._mountModel, 4);
            }
            if (this._petModel == null) 
            {
                this._petModel = new SWFPlayer();
                this._petModel.move(120, 270);
                this._petModel.visible = false;
                addChildAt(this._petModel, 5);
            }
            return;
        }

        /* internal  */onTimerEnterFrameHandler(arg1: SecTimer): void
        {
            if (this._selectedTime <= 0) 
            {
                if (this._tileList.selectedIndex < this._itemNum) 
                {
                    this._tileList.selectedIndex = this._tileList.selectedIndex + 1;
                }
                else 
                {
                    this._tileList.selectedIndex = 0;
                }
                this.setShowModel(this._tileList.selectedIndex);
            }
            else 
            {
                this._selectedTime = this._selectedTime + 1;
                if (this._selectedTime >= 10) 
                {
                    this._selectedTime = 0;
                }
            }
            return;
        }

        /* internal  */onTimerCompleteHandler(arg1: SecTimer): void
        {
            arg1.stop();
            arg1.isDelete = false;
            return;
        }

        protected onChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            this.setShowModel(this._tileList.selectedIndex);
            this._selectedTime = 1;
            return;
        }

        protected setShowModel(arg1: int): void
        {
            var loc7=null;
            var loc8=null;
            this.updateLabelItem(arg1);
            var loc1=this._tileList.getItemAt(arg1) as GiftBagInfo;
            var loc2=loc1.item.category;
            var loc3=loc1.item.type;
            if (this._userModel) 
            {
                this._userModel.visible = false;
            }
            if (this._mountModel) 
            {
                this._mountModel.visible = false;
            }
            if (this._petModel) 
            {
                this._petModel.visible = false;
            }
            if (loc2 != ECategory._ECategoryEquip) 
            {
                if (loc2 != ECategory._ECategoryMount) 
                {
                    if (loc2 == ECategory._ECategoryProp && loc3 == EProp._EPropPetEgg) 
                    {
                        loc8 = PetConfig.instance.getItemInfoByCode(loc1.item.effect);
                        this._petModel.load(ModelUtil.getModelSWF(loc8.pet.modelId), ModelType.Monster, loc8);
                        this._petModel.visible = true;
                        this._petModel.updateFrame(this._petModel.currentAction, this._petModel.currentDirection);
                        this._modelType = 2;
                    }
                }
                else 
                {
                    this._mountModel.load(loc1.item);
                    this._mountModel.visible = true;
                    this._mountModel.moduleSwf.currentDirection = 8;
                    this._mountModel.moduleSwf.updateFrame(this._mountModel.moduleSwf.currentAction, this._mountModel.moduleSwf.currentDirection);
                    this._modelType = 1;
                }
            }
            else 
            {
                loc7 = new Map<any, any> /* flash.utils.Dictionary */();
                this._userModel.visible = true;
                if (loc3 != EEquip._EEquipWing) 
                {
                    if (loc3 == EEquip._EEquipFashion) 
                    {
                        loc7[EEntityAttribute._EAttributeClothes] = loc1.code;
                    }
                }
                else 
                {
                    loc7[EEntityAttribute._EAttributeWing] = loc1.code;
                }
                this._userModel.updateEquipByDic(loc7);
                this._userModel.direction = DirectionType.South;
                this._modelType = 0;
            }
            this._objItem.itemData = loc1.item;
            var loc4=ColorConfig.instance.getItemColorString(loc1.item.color);
            this._nameTxt.htmlText = HTMLUtil.addColor(loc1.item.itemInfo.item.name, loc4);
            var loc5=this._infoMap[loc1.code] as TOpenRechargeGiftBag;
            var loc6=new ItemData(loc5.itemCode);
            this._tipsTxt.htmlText = Language.getStringByParam(31326, loc6.name);
            return;
        }

        public setSelectedByBagCode(arg1: int): void
        {
            var loc2=null;
            var loc1=this._codeMap[arg1];
            var loc3=0;
            while (loc3 <= this._itemNum) 
            {
                loc2 = this._tileList.getItemAt(loc3) as GiftBagInfo;
                if (loc2 && loc1 == loc2.code) 
                {
                    this._tileList.selectedIndex = loc3;
                    this.setShowModel(loc3);
                    this._selectedTime = 1;
                    return;
                }
                ++loc3;
            }
            return;
        }

        public startTimer(): void
        {
            var loc1=0;
            if (this._secTimer) 
            {
                loc1 = RechargeData.restTime;
                this._secTimer.reset();
                this._secTimer.repeatCount = loc1;
                this._secTimer.start();
                this._selectedTime = 0;
            }
            return;
        }

        public stopTimer(): void
        {
            if (this._secTimer) 
            {
                this._secTimer.stop();
                this._secTimer.isDelete = false;
            }
            return;
        }

        public onRechargeGiftBagPanelItemSelectHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            this.setSelectedByBagCode(loc1);
            return;
        }

        protected /* var */_tileList: GTileList;

        protected /* var */_userModel: StatueUserPlayer;

        protected /* var */_petModel: SWFPlayer;

        protected /* var */_mountModel: MountModelPanel;

        protected /* var */_nameTxt: GTextFiled;

        protected /* var */_tipsTxt: GTextFiled;

        protected /* var */_sex: int;

        protected /* var */_camp: int;

        protected /* var */_career: int;

        protected /* var */_modelType: int=0;

        protected /* var */_infoMap: Map<any, any> /* flash.utils.Dictionary */;

        protected /* var */_secTimer: SecTimer;

        protected /* var */_itemNum: int;

        protected /* var */_selectedTime: int=0;

        protected /* var */_codeMap: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_labelIdxArr: Array<any>;

        /* internal */ /* var */_objItem: ObjectItem;

        /* internal */ /* var */_labelItems: Array<any>;
    }
